<!DOCTYPE html>
<body>
<head>
    <meta charset="utf-8">
    <title>自定义对话框预览</title>
    <#include "/pubHeadStyleTree.html">
        <script src="${ctx}/formStatic/form/CommonDialog.js"></script>
        <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
        <link rel="stylesheet" href="${ctx}/qbdmsStatic/iconfont/iconfont.css"/>
        <!--<link rel="stylesheet" href="/qbdmsStatic/style/forms/currencyCss.css">-->
        <link rel="stylesheet" href="${ctx}/qbdmsStatic/style/qbdms.css">
        <style>
            .layui-table-view .layui-laypage .layui-laypage-curr .layui-laypage-em {
                background-color: #2A82FE !important;
            }
        </style>
        </head>
<body>
<input type="hidden" name="selectData">
<input type="hidden" name="selectDataIds">
<div class="ccpui-tablesize" style="padding:10px;position: relative">
    <table id="formDialog" lay-filter="formDialog"></table>
</div>
<#if bpmFormDialog.style==1>
    <div id="dialogTree" class="ztree"></div>

    <#else>
        <script type="text/html" id="formDialogToolsBar">

            <form class="layui-form" action="" lay-filter="formDialog_query" onsubmit="return false">
                <div class="layui-form-body selch" style="height: 100%;">
                    <a class="layui-btn layui-btn-sm fillBtn" style="float: right;" lay-event="query">
                        <span>查询</span>
                    </a>
                    <div>
                        <#if bpmFormDialog.conditionList?? && (bpmFormDialog.conditionList?size > 0) >
                            <#list bpmFormDialog.conditionList as item>
                                <#if item.defaultType==1>
                                    <div class="showDialogQuery">
                                        <label class="layui-form-label outofHiding" title="${item.comment }">${item.comment
                                            }</label>
                                        <#if item.fieldType=='date'>
                                            <#if item.condition=='='>
                                                <div class="layui-input-block" style="">
                                                <input type="text" name="Q_${item.fieldName}_DL"
                                                       class="layui-input start_time filter-value"/>
                                                </div>
                                                <#elseif item.condition=='>='>
                                                    <div class="layui-input-block" style="">
                                                    <input type="text" name="Q_start${item.fieldName}_DL"
                                                           class="layui-input start_time filter-value"/>
                                                    </div>
                                                    <#elseif item.condition=='<='>
                                                    <div class="layui-input-block" style="">
                                                    <input type="text" name="Q_end${item.fieldName}_DG"
                                                           class="layui-input end_time filter-value"/>
                                                    </div>
                                                    <#else>
                                                        <div class="layui-input-block" style="">
                                                        <input type="text" name="Q_start${item.fieldName}_DL"
                                                               class="layui-input filter-value start_time"/>
                                                        </div>
                                                        <label class="layui-form-label">到:</label>
                                                        <div class="layui-input-block" style="">
                                                        <input type="text" name="Q_end${item.fieldName}_DG"
                                                               class="layui-input filter-value end_time"/>
                                                        </div>
                                            </#if>
                                            <#elseif item.fieldType=='number'>
                                                <div class="layui-input-block" style="">
                                                <input type="text" name="Q_${item.fieldName}_DB" class="layui-input"/>
                                                </div>
                                                <#else>
                                                    <#if item.paraCt=='1'>
                                                        <div class="layui-input-block" style="">
                                                        <input type="text" name="${item.fieldName }"
                                                               class="layui-input filter-value"/>
                                                        </div>
                                                    </#if>
                                        </#if>
                                        <#if item.fieldType!='date'>
                                            <#if item.paraCt=='0'>
                                                <input type="hidden" name="selectIds">
                                                <div class="layui-input-block" style="">
                                                <input type="text" readonly="readonly" class="layui-input filter-value"
                                                       name="${item.fieldName}">
                                                </div>
                                                <div class="layui-input-block" style="">
                                                <input type="button" class="layui-btn layui-btn-sm" value="选择"
                                                       onclick="selectCustomDialog(this);" id="dialog"
                                                       dialog="${item.dialog}" param="${item.param}"
                                                       style="margin: 4px;width:48px;">
                                                </div>
                                                <#elseif item.paraCt=='4'>
                                                    <div class="layui-input-block" style="">
                                                    <input type="text" readonly="readonly"
                                                           class="layui-input filter-value" name="${item.fieldName}">
                                                    </div>
                                                    <div class="layui-input-block" style="">
                                                    <input type="button" class="layui-btn layui-btn-sm" value="选择"
                                                           onclick="selectUser(this);" style="margin: 4px;width:48px;">
                                                    </div>
                                                    <#elseif item.paraCt=='17'>
                                                        <div class="layui-input-block" style="">
                                                        <input type="text" readonly="readonly"
                                                               class="layui-input filter-value"
                                                               name="${item.fieldName}">
                                                        </div>
                                                        <div class="layui-input-block" style="">
                                                        <input type="button" class="layui-btn layui-btn-sm" value="选择"
                                                               onclick="selectRole(this);"
                                                               style="margin: 4px;width:48px;">
                                                        </div>
                                                        <#elseif item.paraCt=='18'>
                                                            <div class="layui-input-block" style="">
                                                            <input type="text" readonly="readonly"
                                                                   class="layui-input filter-value"
                                                                   name="${item.fieldName}">
                                                            </div>
                                                            <div class="layui-input-block" style="">
                                                            <input type="button" class="layui-btn layui-btn-sm"
                                                                   value="选择" onclick="selectOrg(this);"
                                                                   style="margin: 4px;width:48px;">
                                                            </div>
                                                            <#elseif item.paraCt=='19'> <!--岗位-->
                                                                <div class="layui-input-block" style="">
                                                                <input type="text" readonly="readonly"
                                                                       class="layui-input filter-value"
                                                                       name="${item.fieldName}">
                                                                </div>
                                                                <div class="layui-input-block" style="">
                                                                <input type="button" class="layui-btn layui-btn-sm"
                                                                       value="选择" onclick="selectUser(this);"
                                                                       style="margin: 4px;width:48px;">
                                                                </div>
                                            </#if>
                                    </div>
                                </#if>
                        </#if>
                        </#list>
                        </#if>
                        <input type="hidden" name="dialog_alias_" value="${bpmFormDialog.alias}">
                    </div>
                </div>
            </form>
        </script>
</#if>
<textarea id="txtJsonData" style="display:none"></textarea>
<input style="display:none" id="parentIndex"/>
<script>
    var needPage = true;
    if ('${bpmFormDialog.needpage}' == 0) {
        needPage = false;
    }
    var setter;
    var isOpenJsonData = '${isOpenJsonData}';
    layui.use(['index', 'commons', 'table', 'layer', 'jquery', 'form', 'tree', 'common', 'totalFilter', 'setter', 'code'], function () {
        var table = layui.table, layer = layui.layer, commons = layui.commons,
            common = layui.common, totalFilter = layui.totalFilter, form = layui.form, code = layui.code;
        common.tableSort("formDialog");
        layui.code();
        setter = layui.setter;

        var selList = [];
        if (layui.selList) {
            selList.push.apply(selList, layui.selList);
        }
        if ('${bpmFormDialog.style}' === '0') {
            //组装列表显示字段数据
            var displayField = JSON.parse('${bpmFormDialog.displayfield}');
            var sortfield = JSON.parse('${bpmFormDialog.sortfield}');
            var isSingle = '${bpmFormDialog.issingle}';
            var array = [];
            var checkboxField = {type: 'checkbox', fixed: 'left'};
            if (isSingle === '1') {
                checkboxField = {type: 'radio', fixed: 'left'};
            }
            array.push(checkboxField);
            for (var i = 0; i < displayField.length; i++) {
                var json = {};
                var fieldName = displayField[i].field;
                json.field = fieldName;
                json.title = displayField[i].comment;
                json.align = "center";
                for (var k = 0; k < sortfield.length; k++) {
                    if (sortfield[k].field == fieldName) {
                        json.sort = true;
                    }
                }
                json.minWidth = 100
                array.push(json);
            }

            //渲染表格
            table.render({
                elem: '#formDialog'
                ,
                cellMinWidth: 80
                ,
                title: '自定义对话框表'
                ,
                defaultToolbar: []
                ,
                height: 'full-30'
                ,
                url: serverPath.formPath + setter.formLinkPath + 'formDialog/showTableData?alias=' + '${alias}' + '${paramStr}'
                ,
                toolbar: '#formDialogToolsBar'
                ,
                page: {limit: '${bpmFormDialog.pagesize}', page: needPage}
                ,
                limits: [5, 10, 15, 20, 50]
                ,
                limit: '${bpmFormDialog.pagesize}'
                ,
                autoSort: false
                ,
                cols: [array]
                ,
                parseData: function (res) {
                    var _data = res.data;
                    //获取主键
                    var count = 0;
                    var pkStr = "ID";
                    for (var p in _data[0]) {
                        count++;
                        var str = _data[0][p];
                        if (str != null && str != "") {
                            if (count == 1 && (str.length == 32 || str == 'orgResourcesManager') && p.indexOf("ID") > 0) {
                                pkStr = p;
                                break;
                            }
                        }
                    }
                    if (_data.length > 0 && selList.length > 0) {
                        for (var i = 0; i < _data.length; i++) {
                            var _d = _data[i];
                            for (var j = 0; j < selList.length; j++) {
                                var _td = selList[j];
                                if (_d[pkStr] === _td.ID) {
                                    _d.LAY_CHECKED = true;
                                }
                            }
                        }
                    }
                }
            });
            totalFilter.init('formDialog');


            table.on('toolbar(formDialog)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data; //获取选中行数据
                switch (obj.event) {
                    case 'query':
                        common.tableConditionRender("formDialog", "formDialog_query", {where: {}}, false, false, '${bpmFormDialog.pagesize}');
                        commons.reflashDate({startName: '.start_time', endName: '.end_time'});
                        break;
                }
            });

            window.Initialization = function () {
                commons.reflashDate({startName: '.start_time', endName: '.end_time'});
            };
            /*刷新时间控件*/
            commons.reflashDate({startName: '.start_time', endName: '.end_time'});
            common.tableSort("formDialog", {show: window.Initialization});

            //获取选中数量
            function getRadioCheckCount() {
                var count = 0;
                var isSingle = '${bpmFormDialog.issingle}';
                if (isSingle === '1') {
                    var radios = $("input[lay-type='layTableRadio']");
                    radios.each(function () {
                        if ($(this).prop("checked") === true) {
                            count++
                        }
                    });
                } else {
                    var item = $("input[name='layTableCheckbox']");
                    item.each(function () {
                        if ($(this).prop("checked") === true) {
                            count++
                        }
                    });
                }
                return count;
            }
        }

        //树的处理开始====
        var zTree;
        var json = JSON.parse('${bpmFormDialog.displayfield}');
        var isSingle = '${bpmFormDialog.issingle}';
        var radioCheckboxFlag = false;
        if (isSingle === '0') {
            radioCheckboxFlag = true;
        }
        if ('${bpmFormDialog.style}' === '1') {
            loadTree();
        }

        //加载树
        function loadTree() {
            var setting = {
                async: {
                    enable: true,
                    url: serverPath.formPath + setter.formLinkPath + 'formDialog/getTreeData?alias=' + '${alias}' + '&isRoot=1',
                    autoParam: [json.id + "=idKey", json.pid + "=pidKey", json.displayName + "=nameKey"],
                    otherParam: {idKeyName: json.id, pidKeyName: json.pid, nameKeyName: json.displayName}
                },
                data: {
                    key: {name: json.displayName},
                    simpleData: {
                        enable: true,
                        idKey: json.id,
                        pIdKey: json.pid
                    }
                },
                check: {
                    enable: radioCheckboxFlag
                },
                callback: {
                    onClick: getClickData
                }
            };
            zTree = $.fn.zTree.init($('#dialogTree'), setting);
        }

        //取消
        $("#cancel").click(function () {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        });
        //重置
        $("#reset").click(function () {
            zTree.checkAllNodes(false);
        });

        //返回字段值
        var resultField = JSON.parse('${bpmFormDialog.resultfield}');
        var resultSingleJson = {};

        //树节点单击事件
        function getClickData(event, treeId, treeNode) {
            for (var j = 0; j < resultField.length; j++) {
                var returnField = resultField[j].field;
                resultSingleJson[returnField] = treeNode[returnField];
            }
            $("#txtJsonData").val(JSON.stringify(resultSingleJson));
        }

        //打开json数据弹框
        function openQueryJsonDialog() {
            parent.layer.open({
                type: 2,
                area: ["550px", '375px'],
                title: ['提示'],
                content: serverPath.formPath + setter.formLinkPath + "formDialog/view/showJson?parentName=" + window.name,
                success: function (layero) {
                    var mask = $(".layui-layer-shade");
                    mask.appendTo(layero.parent());
                }
            });
        }

        //确定事件
        window.determineQuery = function () {
            if ('${bpmFormDialog.style}' === '1') {
                if (radioCheckboxFlag) { //多选
                    var returnData = [];
                    var treeObj = $.fn.zTree.getZTreeObj("dialogTree"),
                        nodes = treeObj.getCheckedNodes(true);
                    for (var i = 0; i < nodes.length; i++) {
                        if (!nodes[i].getCheckStatus().half) {
                            var resultJson = {};
                            for (var j = 0; j < resultField.length; j++) {
                                var returnField = resultField[j].field;
                                resultJson[returnField] = nodes[i][returnField];
                            }
                            returnData.push(resultJson);
                        }
                    }
                    if (JSON.stringify(returnData) === "[]") {
                        layer.alert("还没有选择数据项！");
                        return false;
                    }
                    $("#txtJsonData").val(JSON.stringify(returnData));
                } else {
                    if (JSON.stringify(resultSingleJson) === "{}") {
                        layer.alert("还没有选择数据项！");
                        return false;
                    }
                }
                $("input[name='selectData']").val($("#txtJsonData").val());
                if (isOpenJsonData) {
                    openQueryJsonDialog();
                }
                $("#parentIndex").val(parent.layer.getFrameIndex(window.name));
            } else {
                var checkStatus = table.checkStatus('formDialog'), rowData = checkStatus.data;
                if (getRadioCheckCount() <= 0) {
                    layer.alert("<span class='confirm-title'>提示</span><p>还没有选择数据项！</p>",{title: false,skin: 'confirm-class'}, function (index) {
                        layer.close(index);
                    });
                    return false;
                }
                $("#parentIndex").val(parent.layer.getFrameIndex(window.name));
                var isSingle = '${bpmFormDialog.issingle}';
                var resultField = JSON.parse('${bpmFormDialog.resultfield}');
                //获取主键
                var count = 0;
                var pkStr = "ID";
                for (var p in rowData[0]) {
                    count++;
                    if (count == 1 && (rowData[0][p].length == 32 || rowData[0][p] == 'orgResourcesManager') && p.indexOf("ID") > 0) {
                        pkStr = p;
                        break;
                    }
                }
                if (isSingle === '0') { //多选
                    var returnData = [];
                    var ids = [];
                    for (var k = 0; k < rowData.length; k++) {
                        ids.push(rowData[k][pkStr]);
                        var resultJson = {};
                        for (var j = 0; j < resultField.length; j++) {
                            var returnField = resultField[j].field;
                            resultJson[returnField] = rowData[k][returnField];
                            if (j == resultField.length - 1) {
                                returnData.push(resultJson);
                            }
                        }
                    }
                    $("#txtJsonData").val(JSON.stringify(returnData));
                    $("input[name='selectDataIds']").val(ids);
                } else {
                    var resultJson = {};
                    for (var p = 0; p < resultField.length; p++) {
                        resultJson[resultField[p].field] = rowData[0][resultField[p].field];
                    }
                    $("#txtJsonData").val(JSON.stringify(resultJson));
                    $("input[name='selectDataIds']").val(rowData[0][pkStr]);
                }
                $("input[name='selectData']").val($("#txtJsonData").val());
                if (isOpenJsonData) {
                    openQueryJsonDialog();
                }
            }
        };
    });
</script>
<style>
    .layui-table-tool {
        position: inherit;
    }

    #LAY_app {
        position: relative;
    }
.showDialogQuery{
    margin-right: 100px;
}
    .confirm-class .layui-layer-ico{
        background-position:  -149px -31px;
    }
    .outofHiding {
        width: 110px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
</style>
</body>
</html>
